import tool from '@/utils/tool';
import pub from './public.js';

export default {
    _caparams: {},
    login(caparams, cakeyid, userno, allowbindingcabyself, username, multipleCA_flag) {
        return new Promise((s, f) => {
            this._caparams = caparams;
            if (typeof jsObj != "undefined") {
                console.log('caparams', caparams);
                let chkRetjson = "";
                if (caparams.caclassname.toUpperCase() == "RMINTERFACE.CASZT.BLLCA") {
                    chkRetjson = jsObj.CALoginParas(caparams.caclassname, caparams.caparm1, caparams.caparm2, caparams.caparm3, caparams.caparm4, caparams.caparm5, cakeyid, "");
                } else {
                    chkRetjson = jsObj.CALoginParasEx(caparams.caclassname, caparams.caparm1, caparams.caparm2, caparams.caparm3, caparams.caparm4, caparams.caparm5, cakeyid, "", "1");
                }
                let chkRet = JSON.parse(chkRetjson);
                if (chkRet.code == "-2" || chkRet.code == "-1" || chkRet.code == "-3" || chkRet.code == "-9") {
                    s({
                        flag: "-1",
                        msg: "CA登录失败：" + chkRet.keyid
                    })
                } else {
                    //北京协同CA特殊处理
                    if(caparams.caclassname == "RMInterface.CACOSS.bllCA" || caparams.caclassname == "RMInterface.CACOSSxBJ.bllCA"){
                        let cakeyidList = cakeyid.split('^');
                        if(cakeyidList.length == 2){
                            cakeyid = cakeyidList[1];
                        }
                    }
                    console.log('cakeyid', cakeyid);
                    //  多CA兼容运行 hiscode2用|拼接多个平台认证码
                    if ((multipleCA_flag == '0' && chkRet.keyid == cakeyid) || (multipleCA_flag == '1' && cakeyid.indexOf(chkRet.keyid) > -1) || (allowbindingcabyself == "1" && chkRet.username && chkRet.username == username)) {
                        tool.request({
                            url: "/ucent/login",
                            method: "post",
                            data: {
                                action: "savecaimg",
                                signimg: chkRet.signimg,
                                userno: userno,
                                cakeyid: chkRet.keyid
                            },
                        }).then((res) => {
                            s({
                                flag: res.flag,
                                msg: res.msg
                            })
                        }).catch((e) => {
                            s({
                                flag: "-1",
                                msg: "登录失败，保存用户签名图片过程中发生异常！"
                            })
                        });
                    } else {
                        if (!cakeyid) {
                            s({
                                flag: "-1",
                                msg: "没有维护用户对应的数字证书编号！"
                            })
                        } else {
                            jsObj.Log('用户与数字证书不对应3: ' + userno + ' CA返回:' + chkRet.keyid + '  维护:' + cakeyid);
                            s({
                                flag: "-1",
                                msg: "用户与数字证书不对应！"
                            })
                        }
                    }
                }
            } else {
                s({
                    flag: "0",
                    msg: "系统已启用CA,请在" + pub.clientname + "登录！"
                })
            }
        });
    },
    _userkeyid: [],
    getkeyid(userno) {
        return new Promise((s, f) => {
            let uk = this._userkeyid.find(t => t.userno == userno);
            //console.log('uk', uk);
            if (uk) {
                s({
                    flag: "1",
                    msg: "",
                    keyid: uk.keyid
                });
            } else {
                // console.log('22222');
                tool.request({
                    url: "/ucent/ca",
                    method: "post",
                    data: {
                        action: "getkeyid",
                        userno: userno,
                    },
                }).then((res) => {
                    if (res.flag == 1) {
                        let usercaid = res.hiscode2;
                        this._caparams = res.caparas;
                        this._userkeyid.push({
                            userno: userno,
                            keyid: usercaid
                        });
                        s({
                            flag: "1",
                            msg: "",
                            keyid: usercaid
                        });
                    } else {
                        s({
                            flag: res.flag,
                            msg: res.msg
                        })
                    }
                }).catch((e) => {
                    s({
                        flag: "-1",
                        msg: "CA验证失败，获取用户CA编号过程中发生异常！"
                    })
                });
            }
        });
    },
    checklogin(oper, userno) {
        return new Promise((s, f) => {
            //console.log('checklogin-userno', userno);
            try{
                if (typeof jsObj != "undefined") {
                    this.getkeyid(userno).then((res) => {
                        console.log('userno', userno);
                        console.log('res', res);
                        console.log("this._caparams", this._caparams)
                        if (res.flag == "1") {
                            if (this._caparams.nohiscode2noca == "1" && !res.keyid) {
                                s({
                                    flag: "1",
                                    msg: ""
                                })
                                return;
                            }
                            console.log('this._caparams', this._caparams);
                            var chkRetjson = jsObj.CACheckParas(this._caparams.caclassname, this._caparams.caparm1, this._caparams.caparm2, this._caparams.caparm3, this._caparams.caparm4, this._caparams.caparm5, res.keyid, "")
                            // var chkRetjson = '{"code":"1","keyid":"' + res.keyid + '","signdatas":"aaaaaaaaaaa","signid":"signid11"}';
                            let chkRet = JSON.parse(chkRetjson);
                            //console.log('chkRet', chkRet);
                            if (chkRet.code == "-2" || chkRet.code == "-1" || chkRet.code == "-3" || chkRet.code == "-9") {
                                s({
                                    flag: "-1",
                                    msg: "CA登录失败：" + chkRet.keyid
                                })
                            } else {
                                if ((chkRet.keyid != res.keyid && this._caparams.multipleCA_flag == '0') || (res.keyid.indexOf(chkRet.keyid) == -1 && this._caparams.multipleCA_flag == '1')) {
                                    if (!res.keyid) {
                                        s({
                                            flag: "-1",
                                            msg: "没有维护用户对应的数字证书编号！"
                                        })
                                    } else {
                                        jsObj.Log('用户与数字证书不对应: ' + userno + ' CA返回:' + chkRet.keyid + '  维护:' + res.keyid);
                                        s({
                                            flag: "-1",
                                            msg: "用户与数字证书不对应！"
                                        })
                                    }
                                }
                                else {
                                    tool.request({
                                        url: "/ucent/login",
                                        method: "post",
                                        data: {
                                            action: "savecaimg",
                                            signimg: chkRet.signimg,
                                            userno: userno
                                        },
                                    }).then((res) => {
                                        // s({
                                        //     flag: res.flag,
                                        //     msg: res.msg
                                        // })
                                    }).catch((e) => {
                                        // s({
                                        //     flag: "-1",
                                        //     msg: "登录失败，保存用户签名图片过程中发生异常！"
                                        // })
                                    }); //异步调用存储签名图片，不影响审核速度
                                    s({
                                        flag: "1",
                                        msg: ""
                                    })
                                }
                            }
                        } else {
                            s({
                                flag: res.flag,
                                msg: res.msg
                            })
                        }
                    })
                } else {
                    s({
                        flag: "0",
                        msg: "系统已启用CA,请在" + pub.clientname + "登录！"
                    })
                }
            }
            catch(e){
                console.log('CA登录异常',e);
                s({
                    flag: "-1",
                    msg: "CA登录异常"
                })
            }
        });
    },
    CAsign(userno, reportdata, reportid, infokind = '') {
        return new Promise((s, f) => {
            try{
                if (typeof jsObj != "undefined") {
                    this.getkeyid(userno).then((res) => {
                        // console.log('userno', userno);
                        // console.log('reportdata', reportdata);
                        if (res.flag == "1") {
                            //console.log('this._caparams', this._caparams);
                            if (this._caparams.nohiscode2noca == "1" && !res.keyid) {
                                s({
                                    flag: "1",
                                    msg: ""
                                })
                                return;
                            }
                            // var chkRetjson = jsObj.CALisSignDatasParas(this._caparams.caclassname, this._caparams.caparm1, this._caparams.caparm2, this._caparams.caparm3, this._caparams.caparm4, this._caparams.caparm5, res.keyid, "", reportdata,reportid,"savecasign");
                            // console.log('签名调用结束');
                            // s({
                            //     flag: "1",
                            //     msg: "",
                            //     // keyid: chkRet.keyid,
                            //     // signid: chkRet.signid,
                            //     // signdatas: chkRet.signdatas,
                            // })
                            var chkRetjson = jsObj.CASignDatasParas(this._caparams.caclassname, this._caparams.caparm1, this._caparams.caparm2, this._caparams.caparm3, this._caparams.caparm4, this._caparams.caparm5, res.keyid, "", reportdata);
                            //var chkRetjson = '{"code":"1","keyid":"123","signdatas":"aaaaaaaaaaa","signid":"signid11"}';
                            let chkRet = JSON.parse(chkRetjson);
                            //console.log('chkRet', chkRet);
                            jsObj.Log("报告ID"+reportid+"，CA返回"+ chkRet.code + chkRet.keyid);
                            if (chkRet.code != "1") {
                                s({
                                    flag: "-1",
                                    msg: "CA认证失败：" + chkRet.keyid
                                })
                            } else {
                                if ((chkRet.keyid != res.keyid && this._caparams.multipleCA_flag == '0') || (res.keyid.indexOf(chkRet.keyid) == -1 && this._caparams.multipleCA_flag == '1')) {
                                    if (!res.keyid) {
                                        s({
                                            flag: "-1",
                                            msg: "没有维护用户对应的数字证书编号！"
                                        })
                                    } else {
                                        jsObj.Log('用户与数字证书不对应2: ' + userno + ' CA返回:' + chkRet.keyid + '  维护:' + res.keyid);
                                        s({
                                            flag: "-1",
                                            msg: "用户与数字证书不对应！",
                                        })
                                    }
                                } else {
                                    jsObj.Log("CA开始调用后台保存CA签名数据" + reportid);
                                    tool.request({
                                        url: "/ucent/ca",
                                        method: "post",
                                        data: {
                                            action: "savecasign",
                                            signdatas: chkRet.signdatas,
                                            signid: chkRet.signid,
                                            reportid: reportid,
                                            rechkuser: userno,
                                            keyid: chkRet.keyid,
                                            infokind: infokind
                                        },
                                    }).then((sret) => {
                                        jsObj.Log("CA签名数据保存完成" + reportid);
                                        if (sret.flag == 1) {
                                            s({
                                                flag: "1",
                                                msg: "",
                                                keyid: chkRet.keyid,
                                                signid: chkRet.signid,
                                                signdatas: chkRet.signdatas,
                                            })
                                        } else {
                                            s({
                                                flag: "-1",
                                                msg: sret.msg,
                                            })
                                        }
                                    }).catch(() => {
                                        jsObj.Log("CA签名数据保存异常" + reportid);
                                        s({
                                            flag: "-1",
                                            msg: "签名后数据保存失败！",
                                        })
                                    });
                                }
                            }
                        } else {
                            s({
                                flag: res.flag,
                                msg: res.msg
                            })
                        }
                    })
                } else {
                    s({
                        flag: "0",
                        msg: "系统已启用CA,请在" + pub.clientname + "登录！"
                    })
                }
            }
            catch(e){
                jsObj.Log("CA签名数据异常" + reportid);
                s({
                    flag: "-1",
                    msg: "CA认证异常"
                })
            }
        });
    },
    CaSignSync(userno, reportdata, reportid) {
        if (typeof jsObj != "undefined") {
            this.getkeyid(userno).then((res) => {
                if (res.flag == "1") {
                    //console.log('this._caparams', this._caparams);
                    if (this._caparams.nohiscode2noca == "1" && !res.keyid) {
                        return;
                    }
                    jsObj.CALisSignDatasParas(this._caparams.caclassname, this._caparams.caparm1, this._caparams.caparm2, this._caparams.caparm3, this._caparams.caparm4, this._caparams.caparm5, res.keyid, "", reportdata, reportid, "savecasign");
                    //var chkRetjson = '{"code":"1","keyid":"123","signdatas":"aaaaaaaaaaa","signid":"signid11"}';
                }
            })
        }
    },
    savecasign(that, reportid, signjson) {
        //that.$alert(reportid + " CA签名失败，请刷新后重新审核！");
        //console.log(' savecasign reportid',reportid);
        //console.log('savecasign signjson',signjson);
        var jsondata = JSON.parse(signjson);
        var userno = "";
        var userdata = this._userkeyid.find(item => item.keyid == jsondata.keyid);
        if (userdata) userno = userdata.userno;
        if (that.$children[5] && that.$children[5].$children[2] && that.$children[5].$children[2].$refs.rmlabreport) that.$children[5].$children[2].$refs.rmlabreport.SyncCheckSaveCa(signjson, reportid, userno);

    },
}
